package com.example.springinit2.schedule;

import com.alibaba.fastjson.JSON;
import com.example.springinit2.entity.BIRTH_NOTICE;
import com.example.springinit2.service.BirthService;
import com.example.springinit2.service.MsmService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.sql.rowset.BaseRowSet;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;

@Component
public class ScheduledTasks {


  @Resource
  private MsmService msmService;


  private Logger logger = Logger.getLogger("ScheduledTasks");

  private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
  //
  @Resource
  private BirthService birthService;
  @Resource
  private JavaMailSender mailSender;

  @Scheduled(fixedRate = 1*1000*60)
  public void reportCurrentTime() throws MessagingException {
    logger.info("生日消息通知。现在时间：" + dateFormat.format(new Date()));
    List<BIRTH_NOTICE> birthList = birthService.findBirthList();
    for(BIRTH_NOTICE b : birthList){
      String birth ;
      if (Objects.nonNull(birth = b.getOLD_BIRTH())
              &&Objects.equals(getNow(),birth.substring(4,birth.length()))){
        logger.info("发送生日消息通知开始");
        this.sendMail(b);
        this.sendMsm(b);
        logger.info("发送生日消息通知结束");
      }
    }
    logger.info("生日消息通知。现在时间：" + dateFormat.format(new Date()));
  }


  private void sendMail(BIRTH_NOTICE birth_notice) throws MessagingException {

    MimeMessage mimeMessage = mailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setFrom("lees5488@outlook.com");
    //helper.setTo("lzxy12923@163.com");
    helper.setTo(birth_notice.getEMAIL());
    helper.setSubject("主题：生日快乐");
    helper.setText("<html><body>" +
            "<a>"+birth_notice.getNAME()+
            Calendar.getInstance().get(Calendar.YEAR)
            +"岁生日快乐</a>" +
            "<img src=\"cid:weixin\" ></body></html>", true);

    FileSystemResource file = new FileSystemResource(new File
            ("E:\\新建文件夹\\avatar\\birth.jpg"));
    helper.addInline("weixin", file);
    mailSender.send(mimeMessage);
  }

  private void sendMsm(BIRTH_NOTICE birth_notice){
    msmService.sendMsm(birth_notice.getNAME(),birth_notice.getPHONE());
  }

  private String getNow(){
    SimpleDateFormat df = new SimpleDateFormat("MMdd");
    System.out.println(df.format(new Date()));
    return df.format(new Date());
  }

  public static void main(String[] args) {
    Calendar cal=Calendar.getInstance();
    int y,m,d;
    y = (cal.get(Calendar.YEAR));
    m = (cal.get(Calendar.MONTH));
    d = (cal.get(Calendar.DATE));
    String str = ""+y + m + (d<10?"0"+d:d+"");
    System.out.println(str);
    SimpleDateFormat df = new SimpleDateFormat("MMdd");
    System.out.println(df.format(new Date()));
  }
}
